﻿using System;
using System.Collections.Generic;
using System.Text;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.AppHome.Dto;
using FwProject.BrandInfo.Dto;
using FwProject.Comm;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.BrandInfo;
using FwProject.ShoppingMall.Shop;

namespace FwProject.EventHandle
{
    public class ShopTypHandle : IEventHandler<EntityChangedEventData<ShopType>>, IEventHandler<EntityDeletedEventData<ShopType>>, ITransientDependency
    {
        public readonly ICacheManager _catchmanager;

       
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public ShopTypHandle(ICacheManager catchmanager)
        {
            _catchmanager = catchmanager;
            remoteEventBus = NullRemoteEventBus.Instance;
        }
        /// <summary>
        /// 缓存
        /// </summary>
        /// <param name="eventData">Event data.</param>
        public void HandleEvent(EntityChangedEventData<ShopType> eventData)
        {
            ICache cache = _catchmanager.GetCache(nameof(ShopType));
            cache.Remove("list");
           _catchmanager.GetCache(nameof(HomeDto)).Remove("index");
          
            var catcheventDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id ,[RemoteTypeHelp.MODELNAME] =nameof(ShopType) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, catcheventDate);

            /////同时更新父级搜索引擎类别缓存
            //if (eventData.Entity.Isactive == false)
            //{
            //    //类别不可以商品下架
            //    var deleteDate = new RemoteEventData("RemoveTypeByProduct")
            //    {
            //        Data = { ["modelid"] = eventData.Entity.Id, ["modelname"] = nameof(ShopType) }
            //    };
            //    remoteEventBus.Publish("AppProduct", deleteDate);
            //}

            if (eventData.Entity.Parentid != null)
            {
                var ParentidDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
                {
                    Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Parentid, [RemoteTypeHelp.MODELNAME] = nameof(ShopType) }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, ParentidDate);
            }

        }

        /// <summary>
        /// 缓存
        /// </summary>
        /// <param name="eventData">Event data.</param>
        public void HandleEvent(EntityDeletedEventData<ShopType> eventData)
        {
            ICache cache = _catchmanager.GetCache(nameof(ShopType));
            cache.Remove("list");

            var deleteDate = new RemoteEventData(RemoteTypeHelp.TYPEDELETESEARCH)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopType) }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, deleteDate);


            if (eventData.Entity.Parentid != null)
            {
                ///所有商品下架
                var deleteProductDate = new RemoteEventData(RemoteTypeHelp.TYPEREMOVETYPEBYPRODUCT)
                {
                    Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id, [RemoteTypeHelp.MODELNAME] = nameof(ShopType) }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPPRODUCT, deleteProductDate);

                var ParentidDate = new RemoteEventData(RemoteTypeHelp.TYPESEARCH)
                {
                    Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Parentid, [RemoteTypeHelp.MODELNAME] = nameof(ShopType) }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TOPICAPPSYSTEMSEARCH, ParentidDate);
            }
        }
    }
}
